/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.dao.impl;

import com.ivanbiz.dao.KamarDAO;
import com.ivanbiz.model.GLAccount;
import com.ivanbiz.model.Kamar;
import com.ivanbiz.service.HibernateUtil;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author City_Z
 */
public class KamarDAOImpl extends GenericDAOImpl implements KamarDAO {

    @Override
    public String saveKamar(Kamar kamar) throws Exception {
        String status = "";
        try {
            HibernateUtil.beginTransaction();
            Session session = HibernateUtil.getSession();
            session.save(kamar);
            GLAccount glAccount = new GLAccount();
            glAccount.setNama(kamar.getKodeKamar());
            glAccount.setDeskripsi(kamar.getKodeKamar());
            glAccount.setGroupACC("Debitur");
            glAccount.setKode("XXX");
            glAccount.setNoGL(kamar.getKos().getKodeKos() + kamar.getKodeKamar());
            glAccount.setNameGL(kamar.getKodeKamar());
            session.save(glAccount);
            HibernateUtil.commitTransaction();
            status = "sukses";
        } catch (Exception e) {
            status = "error";
            HibernateUtil.rollbackTransaction();
        } finally {
            HibernateUtil.closeSession();
        }
        return status;
    }

    @Override
    public List<Kamar> getDataByKosAndPenghuni(long id) throws Exception {
        try {
            Session session = HibernateUtil.getSession();
            HibernateUtil.beginTransaction();
            Query query = session.createQuery("from Kamar k where k.kos.id = :id and k.penghuni != null order by k.kodeKamar");
            query.setParameter("id", id);
            HibernateUtil.commitTransaction();
            return query.list();
        } catch (Exception e) {
            HibernateUtil.rollbackTransaction();
            throw e;
        } finally {
            HibernateUtil.closeSession();
        }
    }

}
